<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div><input class="int" type="checkbox"> 刘德华</div>
    <div><input class="int" type="checkbox"> 张学友</div>
    <div><input class="int" type="checkbox"> 郭富城</div>
    <div><input class="int" type="checkbox"> 黎明</div>

    <input class="int2" type="checkbox">
    <button>全选</button>
    <button>全不选</button>
    <button>反选</button>

    <script>
        const input = document.querySelectorAll('input')
        const bnt = document.querySelectorAll('button')
        // 按钮的全选与反选
        bnt[0].addEventListener('click', function () {
            for (let i = 0; i < input.length; i++) {
                input[i].checked = true
            }
        })

        bnt[1].addEventListener('click', function () {
            for (let i = 0; i < input.length; i++) {
                input[i].checked = false
            }
        })

        bnt[2].addEventListener('click', function () {
            for (let i = 0; i < input.length; i++) {
                input[i].checked = !input[i].checked
            }
        })


        // inpt的全选与反选
        const int = document.querySelectorAll('.int')
        const int2 = document.querySelector('.int2')

        int2.addEventListener('change', function () {
            for (let b = 0; b < int.length; b++) {
                int[b].checked = int2.checked
                int[b].addEventListener('change', function () {
                    if (int[b].checked) {
                        int2.checked = true
                    } else {
                        int2.checked = false
                    }
                })
            }
        })

    </script>
</body>

</html>